package com.dgty.dg_common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.viewpager.widget.ViewPager;

/**
 * Title: CompatViewPager$
 * Description:
 * <p>
 * Created DateTime: 2023/2/16$ 9:00$
 * Created by cacif$.
 */
public class CompatViewPager extends ViewPager {

    //是否可以左右滑动
    private boolean isCanScroll = true;

    public CompatViewPager(Context context) {
        super(context);
    }

    public CompatViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            return isCanScroll && super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException e) {
            // IllegalArgumentException: pointerIndex out of range pointerIndex=-1 pointerCount=1
            return false;
        } catch (ArrayIndexOutOfBoundsException e) {
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        /**
         * @see ViewPager#onTouchEvent(MotionEvent) : pointerIndex == -1 代码块
         */
        try {
            return isCanScroll && super.onTouchEvent(event);
        } catch (IllegalArgumentException e) {
            // IllegalArgumentException: pointerIndex out of range pointerIndex=-1 pointerCount=1
            return false;
        }
    }


    public void setScrollable(boolean isCanScroll) {
        this.isCanScroll = isCanScroll;
    }

}

